package com.bjpowernode.learn;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduleTest {
    public static void shc() {
        //获取对象
        ScheduledExecutorService s = Executors.newScheduledThreadPool(1);

        //两秒后执行任务
        s.scheduleAtFixedRate(()->{
            //打印当前线程的名字
            System.out.println(Thread.currentThread().getName());
        },2,2,TimeUnit.SECONDS);

        //关闭
        s.shutdown();
    }

    public static void shc2() {
        //获取对象
        ScheduledExecutorService s = Executors.newScheduledThreadPool(1);
        //两秒后每隔3秒执行一次
        s.scheduleAtFixedRate(()->{
            //打印当前线程的名字
            System.out.println(Thread.currentThread().getName() + ":" + System.currentTimeMillis());
            //模拟耗时操作 6+3=9秒
            try {
                Thread.sleep(6000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },2,3,TimeUnit.SECONDS);
    }

    public static void shc3() {
        //获取对象
        ScheduledExecutorService s = Executors.newScheduledThreadPool(1);
        //两秒后每隔3秒执行一次
        s.scheduleWithFixedDelay(()->{
            //打印当前线程的名字
            System.out.println(Thread.currentThread().getName() + ":" + System.currentTimeMillis());
            //模拟耗时操作 6+3=9秒
            try {
                Thread.sleep(6000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },2,3,TimeUnit.SECONDS);
    }
}
